package uw10.app.data.sources.impl;

import uw10.app.data.Event;
import uw10.app.data.GraphBounds;
import uw10.app.data.GraphData;
import uw10.app.data.GraphIndicator;
import uw10.app.data.sources.EventGraphDataSource;

public class RandomEventGraphDataSource implements EventGraphDataSource
{
	private Event evt;
	private GraphBounds b;
	private GraphData bid;
	private GraphData ask;
	private GraphIndicator[] news;
	private String title;
	
	private void generateData(Event e)
	{
		evt = e;
		b = new GraphBounds(2000, 5000, 100, 200);
		
		int length = 30;
		long[] time = new long[length];
		double[] vBid = new double[length];
		double[] vAsk = new double[length];
		
		GraphBounds b = getGraphBounds(e);
		
		double r = Math.random();
		
		for (int i = 0; i < length; i++)
		{
			r = r * 0.9 + Math.random() * 0.1;
			time[i] = (long)(b.tMin + ((double)i / length) * (b.tMax - b.tMin));
			vBid[i] = (long)(b.vMin + r * (b.vMax - b.vMin));
			vAsk[i] = vBid[i] - Math.random() * 10;
		}
		
		bid = new GraphData(length, time, vBid);
		ask = new GraphData(length, time, vAsk);
		
		news = new GraphIndicator[5];
		for (int i = 0; i < news.length; i++)
		{
			long t = (long) (b.tMin + Math.random() * (b.tMax - b.tMin));
			double v = (bid.getValue(t) + ask.getValue(t)) / 2;
			news[i] = new GraphIndicator(t, v, v + Math.random() * 40 - 20);
		}
		
		title = "STOCK " + (int)(Math.random() * 100) + " AT " + e.hours + ":" + e.minutes;
	}
	
	public GraphBounds getGraphBounds(Event e)
	{
		if (evt != e) generateData(e);
		return b;
	}

	public GraphData getBidData(Event e)
	{
		if (evt != e) generateData(e);
		return bid;
	}

	public GraphData getAskData(Event e) {
		if (evt != e) generateData(e);
		return ask;
	}

	public GraphIndicator[] getNewsIndicators(Event e)
	{
		if (evt != e) generateData(e);
		return news;
	}
	
	public String getTitle(Event e)
	{
		if (evt != e) generateData(e);
		return title;
	}
}
